import 'package:flutter/material.dart';
import 'package:provide/provide.dart';
import 'package:qgchat/page/my/myWallet/customUnderlineTabIndicator.dart' as my;

import '../common/global/global.dart';
import '../states/states_model/user.dart';
import '../widgets/tipsDialog.dart';
import 'live/follow/Follow.dart';
import 'live/hot/Hot.dart';
import 'live/newPeople/NewPeople.dart';
import 'live/square/index.dart';
import 'live/useractivity/index.dart';

class Home extends StatefulWidget {
  Home({Key key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> with TickerProviderStateMixin {
  TabController ctrl;

  final user = [
    IdentityTabBar(
      title: "关注",
      view: Follow(),
    ),
    IdentityTabBar(
      title: "热门",
      view: Hot(),
    ),
    IdentityTabBar(
      title: "新人",
      view: NewPeople(),
    ),
    IdentityTabBar(
      title: "广场",
      view: Square(),
    ),
  ];

  final anchor = [
    IdentityTabBar(
      title: "动态",
      view: Useractivity(),
    ),
    IdentityTabBar(
      title: "热门",
      view: Hot(),
    ),
    IdentityTabBar(
      title: "新人",
      view: NewPeople(),
    ),
    IdentityTabBar(
      title: "广场",
      view: Square(),
    ),
  ];

  @override
  void initState() { 
    super.initState();
    ctrl = TabController(initialIndex: 1, length: 4, vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    ctrl.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final type = Provide.value<UserState>(context)?.info?.memberType;

    final arr = type == 0 ? user : anchor;

    return Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            brightness: Brightness.light,
            // floating: true,
            centerTitle: true,
            actions: [
              IconButton(
                icon: ImageIcon(
                  AssetImage("assets/images/icon/search.png"),
                  color: Colors.black,
                  size: px(34),
                ),
                onPressed: () {
                  final user = Provide.value<UserState>(context);
                  final info = user.info;
                  final isLogin = info != null;
                  if (isLogin) {
                    goPage(context, "/search");
                  } else {
                    TipsDialog.login(
                      context,
                      () {
                        goPage(context, '/phoneLogin');
                      },
                    );
                  }
                },
              )
            ],
            title: Container(
              height: px(60),
              child: TabBar(
                controller: ctrl,
                labelColor: col('303133'),
                unselectedLabelColor: col('909399'),
                labelStyle: TextStyle(
                  fontSize: px(36),
                  fontWeight: FontWeight.bold,
                ),
                unselectedLabelStyle: TextStyle(fontSize: px(30)),
                labelPadding: EdgeInsets.symmetric(horizontal: px(10)),
                isScrollable: true,
                indicator: my.UnderlineTabIndicator(
                  width: px(30),
                  height: px(8),
                  color: col('24d4f2'),
                ),
                tabs: List.generate(
                  arr.length,
                  (i) => Tab(text: arr[i].title),
                ),
              ),
            ),
          ),
          SliverFillRemaining(
            child: TabBarView(
              controller: ctrl,
              children: List.generate(
                arr.length,
                (i) => arr[i].view,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class IdentityTabBar {
  final String title;
  final Widget view;
  IdentityTabBar({
    @required this.title,
    @required this.view,
  });
}
